package com.light.gamecomponents.physics;

import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.RotationModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.light.gamecomponents.gravitylight.GravityLight;
import com.light.roller.ResourceManager;
import com.light.roller.Roller;

public class Fan extends PhysicsComponents implements ContactListener{
	
	Sprite m_sprite2;
	Sprite Black;
	
	int rotation = 0;
	private LoopEntityModifier m_nowModifiler;
	//= 	
	public Fan(Roller game, Vector2 initialPosition) 
	{
		super(game, null, initialPosition, new Vector2(0,0));
	}

	@Override
	public void onUpdate(float pSecondsElapsed) {

	}

	@Override
	public void reset() {
		
	}
	
	@Override
	public void stopBody() 
	{ 
		
		if(m_nowModifiler!=null)m_sprite.unregisterEntityModifier(m_nowModifiler);
		m_nowModifiler = null;
				
	}
	
	@Override
	public void startBody() 
	{ 
		if(m_nowModifiler==null)
		{
			m_nowModifiler = new LoopEntityModifier(99999999, new SequenceEntityModifier(new RotationModifier(1, m_sprite.getRotation(), m_sprite.getRotation()+359)));
			m_sprite.registerEntityModifier(m_nowModifiler);
		}
		
	}

	@Override
	public void onLoadResources() {
		ResourceManager.onLoadResources("FanInside", m_game);
		ResourceManager.onLoadResources("FanOutside", m_game);
		ResourceManager.onLoadResources("Black", m_game);

	}
	
	@Override
	public void unLoadResources() {
		ResourceManager.unLoadResources("FanInside", m_game);
		ResourceManager.unLoadResources("FanOutside", m_game);
		ResourceManager.unLoadResources("Black", m_game);
	}

	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWorld) {
		
		m_sprite = new Sprite(m_initialPosition.x, 
				  		      m_initialPosition.y,
				  		    ResourceManager.getTextureRegion("FanInside"));
		m_sprite2 = new Sprite(m_initialPosition.x, 
	  		      m_initialPosition.y,
	  		    ResourceManager.getTextureRegion("FanOutside"));
		
		Black = new Sprite(m_initialPosition.x+5, 
	  		      m_initialPosition.y+7,
		  		    ResourceManager.getTextureRegion("Black"));

		Black.setSize(m_sprite2.getWidth()-12, m_sprite2.getHeight()-14);
		scene.attachChild(Black);
		scene.attachChild(m_sprite);
		scene.attachChild(m_sprite2);
				
	}	
	
	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) 
	{
		scene.detachChild(Black);
		scene.detachChild(m_sprite);	
		scene.detachChild(m_sprite2);	
	}

	@Override
	public void onLoadEngine() {
		
	}

	@Override
	public void setPosition(int x, int y) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beginContact(Contact contact) {

	}

	@Override
	public void endContact(Contact contact) {
		// TODO Auto-generated method stub
	}

	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}	
	
	@Override
	public boolean isInLight(GravityLight light)
	{
		Vector2 disVec = new Vector2(getSpritePosition().x - light.getSpritePosition().x, 
				 					 getSpritePosition().y - light.getSpritePosition().y);

		float dis = (float)Math.sqrt(disVec.x * disVec.x + disVec.y * disVec.y);
		float minDis = m_sprite.getWidth() / 2 + light.getWidth() / 2 - 15;
		
		if (dis <= minDis)
			return true;
		else
			return false;
		
	}
}